package com.yyfax.ph.personal.mymoney.withdraw;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterLoader;
import com.yyfax.infrastructure.statistics.StatService;
import com.yyfax.ph.R;
import com.yyfax.ph.StatIDSet;
import com.yyfax.ph.base.BaseYYFaxActivity;
import com.yyfax.ph.base.YJPH;
import com.yyfax.ph.base.dialog.DialogBase;
import com.yyfax.ph.common.utils.Constants;
import com.yyfax.ph.common.utils.Utils;
import com.yyfax.ph.web.WebActivity;

/**
 * 提现界面
 * Created by zhangxian on 2017/11/3.<br>
 * Copyright 2017 www.yylending.com. All Rights Reserved.<br><br>
 */
public class WithDrawActivity extends BaseYYFaxActivity implements WithDrawPresenter.View {
	private static final int LOADER_ID_WITHDRAW = 154;

	public static final String MONEY = "money";
	public static final String BANKCODE = "bank_code";
	public static final String BANKCARD = "bank_card";

	@BindView(R.id.money_tv)
	TextView mMoneyTv;
	@BindView(R.id.bank_card_number_tv)
	TextView mBankCardTv;
	@BindView(R.id.withdraw_btn)
	TextView mWithdrawBtn;
	private WithDrawPresenter mPresenter;
	private String mMoneyStr;
	private String mBankCode;
	private String mBankCard;

	public static void start(Activity aty, String moneyStr, String bankCode, String bankCard) {
		Intent intent = new Intent(aty, WithDrawActivity.class);
		intent.putExtra(MONEY, moneyStr);
		intent.putExtra(BANKCODE, bankCode);
		intent.putExtra(BANKCARD, bankCard);
		aty.startActivity(intent);
	}

	@Override
	public Loader<Presenter> onCreateLoader(int id, Bundle args) {
		switch (id) {
			case LOADER_ID_WITHDRAW:
				return new PresenterLoader<>(this, new WithDrawPresenter.Generator());
		}
		return null;
	}

	@Override
	public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {
		switch (loader.getId()) {
			case LOADER_ID_WITHDRAW:
				mPresenter = (WithDrawPresenter) data;
				mPresenter.attach(this);
				break;
		}
	}

	void bindView(String bankCode) {
		mMoneyTv.setText(Utils.getFormatMoney(mMoneyStr));
		if ("0.00".equals(mMoneyStr) || "--".equals(mMoneyStr)) {          //如果传进来的是0或者--, 那么提现按钮置灰
			mWithdrawBtn.setEnabled(false);
		}
		if (mBankCard == null) {
			mBankCard = "    ";
		} else {
			int length = mBankCard.length();
			if (length > 4) {
				mBankCard = mBankCard.substring(length - 4, length);
			}
		}
		mBankCardTv.setText(getString(R.string.bank_card, mBankCard));

		if (TextUtils.isEmpty(bankCode))
			return;
		int imgResId = -1;
		switch (bankCode) {
			case Constants.BANK_CODE_ICBC:
				imgResId = R.drawable.ic_bank_icbc;
				break;
			case Constants.BANK_CODE_ABC:
				imgResId = R.drawable.ic_bank_abc;
				break;
			case Constants.BANK_CODE_BOC:
				imgResId = R.drawable.ic_bank_boc;
				break;
			case Constants.BANK_CODE_CCB:
				imgResId = R.drawable.ic_bank_ccb;
				break;
			case Constants.BANK_CODE_CITIC:
				imgResId = R.drawable.ic_bank_citic;
				break;
			case Constants.BANK_CODE_CEB:
				imgResId = R.drawable.ic_bank_ceb;
				break;
			case Constants.BANK_CODE_HXB:
				imgResId = R.drawable.ic_bank_hxb;
				break;
			case Constants.BANK_CODE_GDB:
				imgResId = R.drawable.ic_bank_gdb;
				break;
			case Constants.BANK_CODE_PINGAN:
				imgResId = R.drawable.ic_bank_pingan;
				break;
			case Constants.BANK_CODE_CIB:
				imgResId = R.drawable.ic_bank_cib;
				break;
			case Constants.BANK_CODE_CMB:
				imgResId = R.drawable.ic_bank_cmb;
				break;
			case Constants.BANK_CODE_SPDB:
				imgResId = R.drawable.ic_bank_spdb;
				break;
			case Constants.BANK_CODE_PSBC:
				imgResId = R.drawable.ic_bank_psbc;
				break;
			case Constants.BANK_CODE_CMBC:
				imgResId = R.drawable.ic_bank_cmbc;
				break;
			case Constants.BANK_CODE_BEIJING:
				imgResId = R.drawable.ic_bank_beijing;
				break;
			default:
				break;
		}
		if (imgResId != -1) {
			Drawable drawable = YJPH.getInstance().getResources().getDrawable(imgResId);
			drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
			mBankCardTv.setCompoundDrawables(drawable, null, null, null);
		}
	}

	@Override
	public int getContentViewRes() {
		return R.layout.withdraw_activity;
	}

	@Override
	public void initContentView() {
		super.initContentView();
		setBtnRightVisible(true);
		setTitleLeftVisible(false);
		setTitleVisible(true);
		setTitle(R.string.withdraw);
	}

	@Override
	public void pretreatmentAfterSetBaseView() {
		super.pretreatmentAfterSetBaseView();
		Intent intent = getIntent();
		if (intent != null) {
			if (intent.hasExtra(MONEY)) {
				mMoneyStr = intent.getStringExtra(MONEY);//待提现的money
			}
			if (intent.hasExtra(BANKCODE)) {
				mBankCode = intent.getStringExtra(BANKCODE);
			}
			if (intent.hasExtra(BANKCARD)) {
				mBankCard = intent.getStringExtra(BANKCARD);
			}
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (savedInstanceState != null) {
			readBundle(savedInstanceState);
		}
		bindView(mBankCode);
		getSupportLoaderManager().initLoader(LOADER_ID_WITHDRAW, null, this);
	}

	private void readBundle(Bundle bundle) {
		mMoneyStr = bundle.getString(MONEY);
		mBankCode = bundle.getString(BANKCODE);
		mBankCard = bundle.getString(BANKCARD);
	}


	@Override
	protected void onDestroy() {
		super.onDestroy();
		getSupportLoaderManager().destroyLoader(LOADER_ID_WITHDRAW);
	}

	@Override
	public void onBackBtnPressed() {
		super.onBackBtnPressed();
		finish();
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		finish();
	}

	@OnCheckedChanged(R.id.wirhdraw_protocol_cb)
	public void OnCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		switch (buttonView.getId()) {
			case R.id.wirhdraw_protocol_cb:
				mWithdrawBtn.setEnabled(!("0.00".equals(mMoneyStr) || "--".equals(mMoneyStr)) && isChecked);
				break;
		}
	}

	@OnClick({R.id.withdraw_btn, R.id.phone_tv, R.id.agreement_tv})
	public void onClilck(View view) {
		super.onClick(view);
		switch (view.getId()) {
			case R.id.withdraw_btn:
				StatService.onEvent(YJPH.getInstance(), StatIDSet.EVENT_ID_E009);
				showTipsDialog(
						getString(R.string.confirm_get_money_back, mMoneyStr),
						R.string.no, R.string.yes,
						new DialogBase.BottomTwoClickListener() {
							@Override
							public void onCancelClick(DialogBase dialog) {
								dialog.dismiss();
							}

							@Override
							public void onConfirmClick(DialogBase dialog) {
								StatService.onEvent(YJPH.getInstance(), StatIDSet.EVENT_ID_E010);
								dialog.dismiss();
								mPresenter.withdraw(mMoneyStr);
							}
						}
				);
				break;
			case R.id.phone_tv:
				showCallDialog(getString(R.string.consumer_hotline_phone));
				break;
			case R.id.agreement_tv:
				WebActivity.start(this, getString(R.string.recommend_service_protocol), WebActivity.TYPE_RECOMMEND_SERVICE_PROTOCOL);
				break;
			default:
				break;
		}
	}

	@Override
	public void withdrawSuccess(String tips) {
		dismissLoading();
		WithDrawSuccessActivity.start(this);
		finish();
	}

	@Override
	public void withdrawFailure(String tips) {
		dismissLoading();
		showToast(tips);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putString(MONEY, mMoneyStr);
		outState.putString(BANKCODE, mBankCode);
		outState.putString(BANKCARD, mBankCard);
	}
}
